package com.kongfuzi.student.ui.usercenter;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.Editable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.kongfuzi.student.R;
import com.kongfuzi.student.app.GasApplication;
import com.kongfuzi.student.support.simple.SimpleTextWatcher;
import com.kongfuzi.student.support.utils.ArgsConstants;
import com.kongfuzi.student.support.utils.RequestUtil;
import com.kongfuzi.student.support.utils.ToastUtil;
import com.kongfuzi.student.support.utils.UrlConstants;
import com.kongfuzi.student.support.utils.Util;
import com.kongfuzi.student.support.volley.Response;
import com.kongfuzi.student.ui.base.CustomerActionBarActivity;

import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;

public class RegisterActivity extends CustomerActionBarActivity implements View.OnClickListener {

    @Bind(R.id.et_number)
    EditText mEt_number;
    @Bind(R.id.et_code)
    EditText mEt_code;
    @Bind(R.id.btn_code)
    Button mBtn_code;
    @Bind(R.id.btn_next)
    Button mBtn_next;
    @Bind(R.id.tv)
    TextView mTv;
    private String mCode = "";
    private TimeCount timeCount;
    private boolean onTick = false;
    private int mType = 1;

    public static Intent newIntent() {
        Intent intent = new Intent(GasApplication.getInstance(), RegisterActivity.class);
        return intent;
    }

    public static Intent startForgetInstance() {
        Intent intent = newIntent();
        intent.putExtra(ArgsConstants.TITLE,"重置密码");
        return intent;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        ButterKnife.bind(this);
        String title = getIntent().getStringExtra(ArgsConstants.TITLE);
        if(!TextUtils.isEmpty(title)) {
            setCustomerTitle(title);
            mType = 2;
        }
        initEvents();
    }

    private void initEvents() {
        timeCount = new TimeCount(60000, 1000);
        mBtn_code.setOnClickListener(this);
        mBtn_next.setOnClickListener(this);
        mBtn_code.setClickable(false);
        mEt_number.addTextChangedListener(new SimpleTextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {
                if (Util.isMoblie(s.toString().trim())) {
                    mBtn_code.setTextColor(getResources().getColor(R.color.text_white));
                    mBtn_code.setBackgroundColor(getResources().getColor(R.color
                            .background_home_actionbar));
                    if (!onTick) {
                        mBtn_code.setClickable(true);
                    }
                } else {
                    mBtn_code.setTextColor(0xFFABADAA);
                    mBtn_code.setBackgroundColor(0xFFF2F5FA);
                    mBtn_code.setClickable(false);
                }
            }
        });
        setCustomerBackOnclickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_code:
                mTv.setVisibility(View.INVISIBLE);
                mBtn_code.setClickable(false);
                timeCount.start();
                onTick = true;
                String requestUrl = UrlConstants.GET_CODE + "&mobile=" + mEt_number.getText()
                        .toString().trim() + "&etype=" + mType;
                RequestUtil.requestGet(requestUrl, new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        if (response != null && response.optBoolean("success")) {
                            mTv.setVisibility(View.VISIBLE);
                            mCode = response.optString("message");
                        } else {
                            timeCount.cancel();
                            timeCount.onFinish();
                        }
                    }
                }, null, mTag);
                break;
            case R.id.btn_next:
                if (!Util.isMoblie(mEt_number.getText().toString().trim())) {
                    ToastUtil.showToast(this,"请输入正确手机号码");
                    return;
                }
                if(TextUtils.isEmpty(mEt_code.getText().toString().trim())) {
                    ToastUtil.showToast(this,"请输入验证码");
                    return;
                }
                if(!mCode.equals(mEt_code.getText().toString().trim())) {
                    ToastUtil.showToast(this, "验证码错误");
                    return;
                }
                GasApplication.putMobileNumber(mEt_number.getText().toString().trim());
                GasApplication.putCaptcha(mCode);
                if(mType == 1) {
                    startActivity(SetPwdActivity.newIntent());
                } else {
                    startActivity(SetPwdActivity.startForgetInstance());
                }
                finish();
                break;
            case R.id.iv_back:
                if(mType == 1) {
                    startActivity(LoginActivity.newIntent());
                }
                finish();
                leftEnter();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        timeCount.cancel();
    }

    class TimeCount extends CountDownTimer {

        /**
         * @param millisInFuture    总时长
         * @param countDownInterval 时间间隔
         */
        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            mBtn_code.setText((int) millisUntilFinished / 1000 + "秒后重发");
        }

        @Override
        public void onFinish() {
            //获取验证码按钮可点击
            if (Util.isMoblie(mEt_number.getText().toString().trim())) {
                mBtn_code.setClickable(true);
            }
            mBtn_code.setText("获取验证码");
            onTick = false;
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
            mIv_back.performClick();
    }
}
